package cn.xizikelif.org.aop;

import cn.xizikelif.org.domain.PerAop;
import cn.xizikelif.org.mapper.PerAopMapper;
import cn.xizikelif.org.service.PerAopService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.util.Date;

@Aspect
@Component
@Slf4j
public class LogAop {

    @Pointcut("@annotation(cn.xizikelif.org.aop.BaseAop)")
    public void pointcut(){}

    @Autowired
    private PerAopMapper perAopMapper;
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        // 执行之前 获取到参数
        // 拦截所有请求，判断出当前请求是什么方法，访问的电脑ip是什么，时间是多少
        MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
        Method method = methodSignature.getMethod();
        // 获取方法上注解里面的值
        BaseAop baseAop = method.getAnnotation(BaseAop.class);
        String name = baseAop.name();
        log.info("注解上面的name:{}",name);
        // 获取方法名称
        String methodName = method.getName();
        log.info("方法名称：{}",methodName);
        // 获取电脑IP
        InetAddress localHost = Inet4Address.getLocalHost();
        String hostAddress = localHost.getHostAddress();
        log.info("电脑的IP地址为：",hostAddress);


        Date itimTim = new Date();
        PerAop perApop = new PerAop(null,name,methodName,hostAddress,itimTim);
        perAopMapper.add(perApop);


        Object proceed = pjp.proceed();

        return proceed;
    }

}
